<template>
  <div class="goods">
    <div class="menu-wrapper" ref="menu">
      <div>
        <ul>
          <li
            class="menu-item"
            v-for="(item,index) of goods"
            :key="index"
            :class="{'current':currentIndex===index}"
            @click="selectMenu(index, $event)"
          >
            <span class="text border-1px">
              <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
              {{item.name}}
            </span>
          </li>
        </ul>
      </div>
    </div>
    <div class="foods-wrapper" ref="foods">
      <div>
        <dl v-for="(item,index) of goods" :key="index" class="foods-list-hook">
          <dt class="item-name">{{item.name}}</dt>
          <dd v-for="(food,index) of item.foods" :key="index" class="item" @click="selectFood(food)">
            <div class="food-icon">
              <img width="57" height="57" :src="food.icon">
            </div>
            <div class="food-content">
              <h1 class="title">{{food.name}}</h1>
              <div v-show="food.description" class="desc">{{food.description}}</div>
              <div class="count">
                <span class="sellCount">月售{{food.sellCount}}份</span>
                <span class="rating">好评率{{food.rating}}%</span>
              </div>
              <div class="price">
                <span class="cur-price"><i>￥</i>{{food.price}}</span>
                <span v-show="food.oldPrice" class="old-price"><i>￥</i>{{food.oldPrice}}</span>
              </div>
              <div class="cartcontrol-wrapper">
                <cart-control :food="food"></cart-control>
              </div>
            </div>
          </dd>
        </dl>
      </div>
    </div>
    <goods-cart :selectFoods="selectFoods" :minPrice="seller.minPrice" :deliveryPrice="seller.deliveryPrice"></goods-cart>
    <food :food="selectedFood" ref="food"></food>
  </div>
</template>
<script>
import axios from 'axios'
import BScroll from 'better-scroll'
import GoodsCart from 'components/cart/Cart'
import CartControl from 'components/cartcontrol/CartControl'
import Food from 'components/food/Food'
export default {
  name: 'Goods',
  props: {
    seller: Object
  },
  components: {
    GoodsCart,
    CartControl,
    Food
  },
  data () {
    return {
      classMap: [],
      goods: [],
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    }
  },
  methods: {
    getGoodsInfo () {
      axios.get('/api/data.json').then(this.getGoodsInfoSuccess)
    },
    getGoodsInfoSuccess (res) {
      const data = res.data
      this.goods = data.goods
      this.$nextTick(() => {
        this._initScroll()
        this._calculateHeight()
      })
    },
    _initScroll () {
      this.menuScroll = new BScroll(this.$refs.menu, {
        click: true
      })
      this.foodsScroll = new BScroll(this.$refs.foods, {
        probeType: 3, // 设置为3会实时告诉我们滚动的位置，相当于一个探针
        click: true
      })
      this.foodsScroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y))
      })
    },
    _calculateHeight () { // 计算高度
      let foodList = document.querySelectorAll('.foods-list-hook')
      let height = 0
      this.listHeight.push(0)
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i]
        height += item.clientHeight
        this.listHeight.push(height) // 把每个区间（dl）的高度保存到数组中
      }
    },
    selectMenu (index, event) {
      if (!event._constructed) {
        return
      }
      let foodList = document.querySelectorAll('.foods-list-hook')
      let el = foodList[index]
      this.foodsScroll.scrollToElement(el, 300)
    },
    selectFood (food) {
      this.selectedFood = food
      this.$refs.food.show()
    }
  },
  computed: {
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let h1 = this.listHeight[i]
        let h2 = this.listHeight[i + 1]
        if (!h2 || (this.scrollY >= h1 && this.scrollY < h2)) {
          return i
        }
      }
      return 0
    },
    selectFoods () {
      let foodsList = []
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            foodsList.push(food)
          }
        })
      })
      return foodsList
    }
  },
  mounted () {
    this.classMap = ['decrease', 'discount', 'guarantee', 'invoice', 'special']
    this.getGoodsInfo()
  }
}
</script>
<style lang="stylus" scoped>
  @import '~common/styles/mixins.styl'
  @import '~common/styles/varibles.styl'
  .goods
    display flex
    position absolute
    top 178px
    left 0
    right 0
    bottom 48px
    overflow hidden
    .menu-wrapper
      flex 0 0 80px
      width 80px
      background-color #f3f5f7
      .menu-item
        display table   // 设置为table，方便垂直居中
        padding 0 12px
        width 56px
        height 54px
        line-height 14px
        &.current
          z-index 10
          position relative
          margin-top -1px
          font-weight 700
          background-color #fff
          .text
            border-none()
        &:last-child
          .text
            border-none()
        .icon
          bg-image-size(12px, 12px)
          &.decrease
            bg-image('decrease_3')
          &.discount
            bg-image('discount_3')
          &.guarantee
            bg-image('guarantee_3')
          &.invoice
            bg-image('invoice_3')
          &.special
            bg-image('special_3')
        .text
          display table-cell
          vertical-align middle
          width 56px
          margin-left 2px
          font-size 12px
          border-1px(rgba(7,17,27,0.1))
    .foods-wrapper
      flex 1
      .item-name
        padding-left 14px
        height 26px
        line-height 26px
        border-left 2px solid #d9dde1
        font-size 12px
        color rgb(147,153,159)
        background-color #f3f5f7
      .item
        position relative
        display flex
        padding 18px 0
        margin 0 18px
        border-bottom 1px solid rgba(7,17,27,.1)
        &:last-child
          border-bottom none
        .food-icon
          flex 0 0 57px
          vertical-align top
          margin-right 10px
          img
            border-radius 2px
        .food-content
          // position relative
          flex 1
          vertical-align top
          font-size 0
          .title
            margin 2px 0 8px 0
            font-size 14px
            font-weight bold
            color rgb(7,17,27)
          .desc
            font-size 10px
            color rgb(147,153,159)
          .count
            margin 8px 0
            line-height 10px
            font-size 10px
            color rgb(147,153,159)
            .sellCount
              margin-right 12px
          .price
            .cur-price
              line-height 14px
              font-size 14px
              color rgb(240,20,20)
              font-weight 700
              margin-right 8px
              i
                font-size 10px
                font-weight normal
            .old-price
              line-height 14px
              font-size 10px
              font-weight 700
              color rgb(147,153,159)
              text-decoration line-through
              i
                font-weight normal
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom 6px
</style>
